package com.aierkeji.call.cms.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aierkeji.call.cms.contants.Contants;
import com.aierkeji.call.cms.service.AkjCallClueService;
import com.aierkeji.call.cms.service.AkjCallLoginService;
import com.aierkeji.call.cms.util.AppConfig;
import com.aierkeji.call.cms.util.ResultMapHelper;

@RestController
@RequestMapping("login")
public class AkjCallLoginController {
	private final Logger log = LogManager.getLogger(getClass());

	@Autowired
	HttpServletRequest HttpServletRequest;
	@Autowired
	AkjCallLoginService akjCallLoginService;
	@Autowired
	AkjCallClueService akjCallClueService;
	

	@PostMapping("/")
	public Map<String, Object> login(@RequestBody Map<String,Object> map) {
		return akjCallLoginService.login(map);
	}
	
	@PostMapping("/getSessionId")
	public Map<String, Object> getSessionId() {
		Session session = SecurityUtils.getSubject().getSession();
		session.setTimeout(Long.parseLong(AppConfig.getProperty("sessionTimeOut", Contants.sessionTimeount.toString())));
		return ResultMapHelper.success(session.getId());
	}
	
	@PostMapping("/getYzm")
	public Map<String, Object> getYzm(@RequestBody Map<String,Object> map) {
		return akjCallLoginService.getYzm(map);
	}
	
	@PostMapping("/loginByYzm")
	public Map<String, Object> loginByYzm(@RequestBody Map<String,Object> map) {
		return akjCallLoginService.loginByYzm(map);
	}
	@GetMapping("logout")
	public Map<String, Object> logout(HttpServletRequest request) {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return ResultMapHelper.success();
	}
}
